﻿// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using System.CodeDom.Compiler;
using System.IO;

namespace FileGenerator
{
    public abstract class CodeGenerator
    {
        private IndentedTextWriter _writer = default!;

        protected CodeGenerator()
        {
        }

        protected CodeGenerator(CodeGenerator parent)
            => _writer = parent._writer;

        protected int Indent
        {
            get => _writer.Indent;
            set => _writer.Indent = value;
        }

        public void CloseWriter()
        {
            _writer.InnerWriter.Dispose();
            _writer.Dispose();
        }

        public void CreateWriter(string fileName)
        {
            Console.WriteLine("Creating: " + fileName);

            var streamWriter = new StreamWriter(fileName);
            _writer = new IndentedTextWriter(streamWriter, "    ");
        }

        protected void Write(char value)
            => _writer.Write(value);

        protected void Write(int value)
            => _writer.Write(value);

        protected void Write(string? value)
            => _writer.Write(value);

        protected void WriteEnd()
            => WriteEndColon();

        protected void WriteElse(string action)
        {
            WriteLine("else");
            Indent++;
            WriteLine(action);
            Indent--;
        }

        protected void WriteEndColon()
        {
            Indent--;
            WriteLine("}");
        }

        protected void WriteIf(string condition, string action)
        {
            WriteLine("if (" + condition + ")");
            Indent++;
            WriteLine(action);
            Indent--;
        }

        protected void WriteLine()
        {
            int indent = Indent;
            Indent = 0;
            _writer.WriteLine();
            Indent = indent;
        }

        protected void WriteLine(string value)
            => _writer.WriteLine(value);

        protected void WriteQuantumType()
        {
            _writer.WriteLine();
            _writer.WriteLine("#if Q8");
            _writer.WriteLine("using QuantumType = System.Byte;");
            _writer.WriteLine("#elif Q16");
            _writer.WriteLine("using QuantumType = System.UInt16;");
            _writer.WriteLine("#elif Q16HDRI");
            _writer.WriteLine("using QuantumType = System.Single;");
            _writer.WriteLine("#else");
            _writer.WriteLine("#error Not implemented!");
            _writer.WriteLine("#endif");
            _writer.WriteLine();
        }

        protected void WriteStart(string namespaceName)
        {
            _writer.WriteLine("// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.");
            _writer.WriteLine("// Licensed under the Apache License, Version 2.0.");
            _writer.WriteLine("// <auto-generated/>");
            _writer.WriteLine("#nullable enable");
            _writer.WriteLine();
            WriteUsing();
            _writer.WriteLine("namespace " + namespaceName);
            WriteStartColon();
        }

        protected void WriteStartColon()
        {
            WriteLine("{");
            Indent++;
        }

        protected virtual void WriteUsing()
        {
        }
    }
}
